<template>
	<view class="content">
		 <u-navbar :is-back="true" title="修改群名称"  title-color="#303233" :title-size="34">
			 <view slot="right" class="u-p-r-30">
				 <u-button @click="updateGroupName" size="mini"  type="success"   :disabled="groupName.length<1">完成</u-button>
			 </view>
		 </u-navbar>
		 <view class="u-p-30">
			  <u-form-item label="群聊名称:" label-position="top" :label-style="{color:'#A5A5A5'}" :border-bottom="false">
				  <u-input v-model="groupName"   @input="onInput" 
					:custom-style="{backgroundColor:'#F2F2F7',borderRadius: '10rpx',padding:'10rpx 30rpx',fontSize:'32rpx'}"
				    :maxlength="maxLength" :clearable="true" :focus="true" :trim="true"
				    placeholder="请输入群名称" />
			 </u-form-item>
		 </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				groupId:"",
				groupName:"",
				maxLength:16
			};
		},
		onLoad:function(e){
			this.groupId=e.groupId;
			this.groupName=e.groupName;
		},
		onBackPress:function(event){
			console.log("监听修改群名称页面回退");
			//this.$store.commit('setPageRefresh',false);
			// #ifndef H5
			 plus.key.hideSoftKeybord();
			// #endif
			uni.hideKeyboard();
		},
		
		methods:{
			onInput:function(val){
				let that=this;
				if(val.length>=16){
					uni.hideKeyboard();
					// #ifndef H5
					  plus.key.hideSoftKeybord();
					// #endif
					this.globalUtil.utilAlert("抱歉,群名称不能超过16个字!");
					return;
				}
			},
			
			updateGroupName:function(){
				let that=this;
				uni.hideKeyboard();
				// #ifndef H5
				  plus.key.hideSoftKeybord();
				// #endif
				if(that.groupName.length<1){
					this.globalUtil.utilAlert("群名称不能为空!","warning");
					return;
				}
				let param={
					"groupId":this.groupId,
					"groupName":this.groupName
				};
				that.localGroupApi.updateGroupInfoFromServer(that,param,(result)=>{
					console.log("修改群聊名称结果",result);
					if(result==true){
						// #ifndef H5
						plus.key.hideSoftKeybord();
						// #endif
						uni.hideKeyboard();
						let timestamp = new Date().getTime();
						let message = {
							"id": new Date().getTime() + "",
							"chatGroupId": that.groupId,
							"userId": that.vuex_userInfo.id,
							"meFlag": true,
							"userName": that.vuex_userInfo.userName,
							"userAvatar": that.vuex_userInfo.avatar ? that.vuex_userInfo.avatar :
								"/static/image/default/default-user/default-user.png",
							'messageType': that.messageApi.MSG_TYPE.SYSTEM_MSG,
							"contentType":  that.messageApi.CONTENT_TYPE.TEXT_CONTENT_TYPE,
							"content": {
								'text':that.vuex_userInfo.userName+"修改群名为\""+that.groupName+"\"",
								'sysMsgType':that.messageApi.SYS_MSG_TYPE.updateGroupName,
								'param':param,
							},
							"createTime": that.globalUtil.getTimeStr(timestamp),
							"timestamp": timestamp,
							"platFrom": 1,
							"showTimeFlag": true,
						}
						console.log("发送修改群名称的系统信息", message);
						this.globalWX.sendMessage(message, {
							complete: function(res) {
								if (res == true) {
									uni.$emit("groupInfoListener",param);
								} else {
									that.globalUtil.utilAlert("网络故障，稍后重试！");
									return;
								}
							}
						},that.messageApi.BUSINESS_CODE.chatting);
						uni.navigateBack({
							delta:1,
							animationType:'pop-out',
							animationDuration:200
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
   .right{
	   margin: 0px 10upx;
   }
</style>
